import {
  checkLogin
} from './auth.js'
new Vue({
  el: '#app',
  data: {
    IS_LOGIN: null,
    searchKey: '', // 搜索的关键词
    name: '',
    // 省列表
    provinceList: [],
    // 选中的省
    checkedProvince: '',
    // 市列表
    cityList: [],
    // 选中的市
    checkedCity: '',
    // 区列表
    districtList: [],
    // 选中的区
    checkedDistrict: '',
    // 收货人
    name: '',
    // 邮编
    zip: '',
    // 详细地址
    address: '',
    // 手机号码
    phone: '',
    // 固定电话
    tel: '',
    // 默认地址
    tag: '',
    // 查询参数
    queryParam: null
  },
  watch: {
    checkedProvince: function (val) {
      this.getCityList()
    },
    checkedCity: function (val) {
      this.getDistrictList()
    },
  },
  async created() {
    this.IS_LOGIN = await checkLogin()
  },
  async mounted() {
    this.$nextTick(async () => {
      await this.getProvinceList()
      this.getQueryParam()
      if (this.queryParam != null) {
        console.log('修改地址');
        this.getAddressInfo()
      }
    })
  },
  methods: {
    // 获取地址信息
    async getAddressInfo() {
      const res = await request.get(`/address/getAddressById/${this.queryParam}`)
      this.checkedProvince = res.data.provinceCode
      setTimeout(() => {
        setTimeout(() => {
          this.checkedCity = res.data.cityCode
        }, 200)
        this.checkedDistrict = res.data.areaCode
      }, 200)
      this.name = res.data.name
      this.zip = res.data.zip
      this.address = res.data.address
      this.phone = res.data.phone
      this.tel = res.data.tel
      this.tag = res.data.tag
    },
    // 获取查询参数
    getQueryParam() {
      const param = location.search.substring(location.search.indexOf('=') + 1)
      this.queryParam = param !== '' ? param : null
      console.log(this.queryParam);
    },
    // 保存
    save() {
      if (this.queryParam == null) {
        this.addAddress();
      } else {
        this.updateAddress();
      }
    },
    // 修改地址
    async updateAddress() {
      console.log('修改地址:');
      if (!this.checkColum()) {
        return
      }
      const res = await request.put('/address/updateAddress', {
        provinceCode: this.checkedProvince,
        cityCode: this.checkedCity,
        areaCode: this.checkedDistrict,
        name: this.name,
        zip: this.zip,
        address: this.address,
        phone: this.phone,
        tel: this.tel,
        tag: this.tag,
        aid: this.queryParam
      })
      this.$message.success(res.msg)
      this.getAddressInfo()
      setTimeout(() => {
        location.href = '/web/address.html'
      }, 1000)
    },
    // 检测必填项
    checkColum() {
      if (this.name == '' || this.zip == '' || this.address == '' || this.phone == '') {
        this.$message.error('请填写完整信息')
        return false
      }
      return true
    },
    // 添加地址
    async addAddress() {
      console.log('添加地址:');
      console.log(this.checkedProvince,
        this.checkedCity,
        this.checkedDistrict,
        this.name, this.zip,
        this.address,
        this.phone,
        this.tel,
        this.tag);
      if (!this.checkColum()) {
        return
      }
      const res = await request.post('/address/addAddress', {
        provinceCode: this.checkedProvince,
        cityCode: this.checkedCity,
        areaCode: this.checkedDistrict,
        name: this.name,
        zip: this.zip,
        address: this.address,
        phone: this.phone,
        tel: this.tel,
        tag: this.tag
      })
      this.$message.success(res.msg)
      this.reset()
    },
    // 重置表单信息
    reset() {
      this.name = ''
      this.zip = ''
      this.address = ''
      this.phone = ''
      this.tel = ''
      this.tag = ''
      this.getProvinceList()
    },
    async getCityList() {
      const res = await request.get('/area/getCityList', {
        params: {
          provinceCode: this.checkedProvince
        }
      })
      this.cityList = res.data
      this.checkedCity = this.cityList[0].code
    },
    async getDistrictList() {
      const res = await request.get('/area/getDistrictList', {
        params: {
          cityCode: this.checkedCity
        }
      })
      this.districtList = res.data
      this.checkedDistrict = this.districtList[0].code
    },
    async getProvinceList() {
      const res = await request.get('/area/getProvinceList')
      this.provinceList = res.data
      this.checkedProvince = this.provinceList[0].code
    },
    // 查找商品
    searchProduct() {
      console.log('查找商品:');
      location.href = `../web/search.html?searchKey=${this.searchKey}`
    },
    // 退出登录
    logout() {
      localStorage.removeItem('token')
      location.href = '/'
    },
  }
})